<template>
  <div>
    <div class="content" v-show="userStatus.dealPwdState=='0'">
      <h3 class="user-title">{{'USER_TRADE_PASS'|t}}</h3>
      <div class="form">
        <zx-form-group :title="$t('PHONEEMAIL')">
          <zx-input v-model="passForm.phone" type="text" :place="$t('PHONEEMAIL_PLACE')" ref="phone" :rules="rules.phoneEmail">
            <span class="input-text" slot="text" @click="getCode(passForm.phone,'phone',$t('GET_CODE'),$event,'phoneCodeBool')">{{'GET_CODE'|t}}</span>
          </zx-input>
        </zx-form-group>
        <zx-form-group :title="$t('CODE_LABEL')">
          <zx-input v-model="passForm.phone_code" :place="$t('CODE_PLACE')" ref="phone_code" :rules="rules.code">
          </zx-input>
        </zx-form-group>
        <zx-form-group :title="$t('PASSWORD')">
          <zx-input v-model="passForm.pass" type="password" :place="$t('PASSWORD_ENTER')" ref="pass" :rules="rules.password">
          </zx-input>
        </zx-form-group>
        <zx-form-group :title="$t('CHECK_PASS')">
          <zx-input v-model="passForm.checkPass" type="password" :place="$t('CHECK_PASS_PLACE')" ref="checkPass" :rules="rules.password">
          </zx-input>
        </zx-form-group>
        <zx-form-group>
          <input type="submit" class="submit-btn mt-20  green-btn radius" :value="$t('COMFIRM')" @click="submitForm('passForm')">
        </zx-form-group>
      </div>
    </div>
    <zx-user-auth :title="$t('SAFE_AND_EFFECTIVE')" routerPath='/user/trade/change' v-show="userStatus.dealPwdState=='1'"></zx-user-auth>
  </div>
</template>
<script>
  import { mapGetters } from 'vuex';
  import { Form, Func, Auth, Rules } from '@/providers';
  import { User, UserInfo } from '@/services';
  import UserAuthText from '@/components/user-auth-text';
  export default {
    data() {
      return {
        name: '',
        phoneCodeBool: false,
        passForm: {
          phone: Auth.getItem('phone'),
          phone_code: '',
          pass: '',
          checkPass: ''
        },
        rules: Rules
      };
    },
    methods: {
      // 获取验证码
      getCode(val, type, innerText, event, clickBool) {
        if (!Form.validItem(this, type)) { return; };
        if (this[clickBool]) { return false; };
        this[clickBool] = true;
        let patternPhone = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
        if (patternPhone.test(val)) {
          Form.SendMobile(val, event.target, clickBool, innerText);
        } else {
          Form.SentMail(val, event.target, clickBool, innerText);
        }
      },
      // 表单提交
      async submitForm(formName, type) {
        if (!Form.validataForm(this, formName)) {
          return false;
        }
        let _parmas = this[formName];
        if (_parmas.pass != _parmas.checkPass) {
          this.$dialog.toast({
            msg: '两次输入密码不一样',
            position: 'top'
          });
          return false;
        }
        let patternPhone = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
        try {
          let res = await User.SetDealPwd({
            password: Func.PasswordEncrypt(_parmas.checkPass),
            type: patternPhone.test(_parmas.phone) ? 1 : 2,
            validataCode: _parmas.phone_code,
            token: Auth.getItem('token')
          });
          if (!Form.checkResToken(res)) return false;
          this.$dialog.toast({
            msg: res.desc,
            position: 'top',
            onOk: () => {
              this.$store.dispatch('GETUSERTATUS');
            }
          });
        } catch (error) {
          console.log(error);
        }
      }
    },
    computed: {
      ...mapGetters([
        'userStatus'
      ])
    },
    components: {
      'zx-user-auth': UserAuthText
    }
  };
</script>
<style scoped lang="scss">
  @import "../../assets/sass/user.scss";
</style>